﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTDeviceUsing : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDevice();
                BindGridView();
            }
        }

        #region Private Method

        private void BindDevice()
        {
            MasterDataService masterDataServiceObj = ServiceFactory.getMasterDataService();
            OperationResult<List<tb_MasterData>> result = masterDataServiceObj.GetMasterDataByTypeId(Int32.Parse(ConfigurationManager.AppSettings["MDDeviceType"].ToString()));


            if (result.operationSuccess)
            {
                ddlDevice.DataSource = result.obj;
                ddlDevice.DataTextField = "Name";
                ddlDevice.DataValueField = "Id";
                ddlDevice.DataBind();
            }
        }

        private void BindGridView()
        {
            RPTDeviceUsingService RPTDeviceUsingServiceObj = ServiceFactory.getRPTDeviceUsingService();

            OperationResult<List<tb_RPT_DeviceUsing>> result = RPTDeviceUsingServiceObj.GetAllRPTDeviceUsing();

            if (result.operationSuccess)
            {
                gvDeviceList.DataSource = result.obj;
                gvDeviceList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            MasterDataService masterDataServiceObj = ServiceFactory.getMasterDataService();
            RPTDeviceUsingService RPTDeviceUsingServiceObj = ServiceFactory.getRPTDeviceUsingService();
            OperationResult<List<tb_MasterData>> result = masterDataServiceObj.GetMasterDataByTypeId(Int32.Parse(ConfigurationManager.AppSettings["MDDeviceType"].ToString()));


            if (result.operationSuccess)
            {
                ddlEditDevice.DataSource = result.obj;
                ddlEditDevice.DataTextField = "Name";
                ddlEditDevice.DataValueField = "Id";
                ddlEditDevice.DataBind();
            }

            OperationResult<tb_RPT_DeviceUsing> resultEdit = RPTDeviceUsingServiceObj.GetRPTDeviceUsingById(id);
            if (result.operationSuccess == true)
            {
                ddlEditDevice.SelectedValue = resultEdit.obj.DeviceId.ToString();
                txtEditUseDate.Text = resultEdit.obj.UsingDate.ToString();
                txtEditStartTime.Text = resultEdit.obj.StartDate.ToString();
                txtEditEndTime.Text = resultEdit.obj.EndDate.ToString();
                txtEditStatus.Text = resultEdit.obj.Status.ToString();
                txtEditTester.Text = resultEdit.obj.Tester.ToString();
                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();

            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        public string GetDeviceNameById(string DeviceId)
        {
            string returnValue = "";
            returnValue = MaxStudio.Concrete.Component.MasterDateString.GetMasterDataNameById(Int32.Parse(DeviceId));
            return returnValue;
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTDeviceUsingService RPTDeviceUsingServiceObj = ServiceFactory.getRPTDeviceUsingService();
            tb_RPT_DeviceUsing instance = new tb_RPT_DeviceUsing();

            instance.DeviceId = Int32.Parse(ddlDevice.SelectedValue.ToString());
            instance.UsingDate = DateTime.Parse(txtUseDate.Text.ToString());
            instance.StartDate = DateTime.Parse(txtStartTime.Text.ToString());
            instance.EndDate = DateTime.Parse(txtEndTime.Text.ToString());
            instance.Status = txtStatus.Text;
            instance.Tester = txtTester.Text;

            OperationResult<tb_RPT_DeviceUsing> result = RPTDeviceUsingServiceObj.CreateRPTDeviceUsing(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTDeviceUsingService RPTDeviceUsingServiceObj = ServiceFactory.getRPTDeviceUsingService();


            tb_RPT_DeviceUsing entity = new tb_RPT_DeviceUsing();

            entity.Id = editId;
            entity.DeviceId = Int32.Parse(ddlEditDevice.SelectedValue.ToString());
            entity.UsingDate = DateTime.Parse(txtEditUseDate.Text.ToString());
            entity.StartDate = DateTime.Parse(txtEditStartTime.Text.ToString());
            entity.EndDate = DateTime.Parse(txtEditEndTime.Text.ToString());
            entity.Status = txtEditStatus.Text;
            entity.Tester = txtEditTester.Text;

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_DeviceUsing> resultEdit = RPTDeviceUsingServiceObj.ModifyRPTDeviceUsing(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTDeviceUsingService RPTDeviceUsingServiceObj = ServiceFactory.getRPTDeviceUsingService();

            tb_RPT_DeviceUsing deleteEntity = new tb_RPT_DeviceUsing();
            deleteEntity.Id = deleteId;
            RPTDeviceUsingServiceObj.DeleteRPTDeviceUsing(deleteEntity);

            BindGridView();
        }

        protected void gvDeviceList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvDeviceList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDeviceList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
